package cn.com.dhc.mibd.eufw.widget.android.widget;

import android.content.Context;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.GridView;

import cn.com.dhc.mibd.eufw.widget.android.ptr.PullToRefreshGridView;

/**
 * Abstract GridView.
 */
public abstract class AbstractGridView {

    /**
     * PullToRefreshGridView
     */
    protected PullToRefreshGridView ptrGridView = null;

    /**
     * gridView
     */
    protected GridView gridView = null;

    /**
     * Footer View
     */
    protected View footerView = null;

    /**
     * AsyncTaskActivityDelegate
     */
    //    protected AsyncTaskActivityDelegate delegate = null;

    protected Context context;

    /**
     * OnScrollListener
     */
    private OnScrollListener onScrollListener = null;

    /**
     * Constructor
     */
    //    public AbstractGridView(AsyncTaskActivityDelegate delegate, GridView gridView) {
    //        this.delegate = delegate;
    //        this.gridView = gridView;
    //        onInject();
    //        onInitialize();
    //    }
    public AbstractGridView(Context context, GridView gridView) {
        this.context = context;
        this.gridView = gridView;
        onInitialize();
    }

    /**
     * Constructor
     */
    //    public AbstractGridView(AsyncTaskActivityDelegate delegate, PullToRefreshGridView ptrGridView) {
    //        this.delegate = delegate;
    //        this.gridView = ptrGridView.getRefreshableView();
    //        this.ptrGridView = ptrGridView;
    //        onInject();
    //        onInitialize();
    //    }
    public AbstractGridView(Context context, PullToRefreshGridView ptrGridView) {
        this.context = context;
        this.gridView = ptrGridView.getRefreshableView();
        this.ptrGridView = ptrGridView;
        onInitialize();
    }

    //    public AsyncTaskActivityDelegate getDelegate() {
    //        return delegate;
    //    }

    public Context getContext() {
        return context;
    }

    /**
     * Inject objects
     */
    //    protected void onInject() {
    //        AbstractApplication.get().inject(this);
    //    }

    /**
     * Initialize GridView
     */
    protected void onInitialize() {

        // Enable Smooth Scroll-bar
        gridView.setSmoothScrollbarEnabled(true);
        gridView.setFastScrollEnabled(false);

        // 短按
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AbstractGridView.this.onItemClickListener(parent, view, position, id);
            }
        });

        // 长按
        gridView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return AbstractGridView.this.onItemLongClickListener(parent, view, position, id);
            }
        });

        // 弹出菜单
        gridView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                AbstractGridView.this.onCreateContextMenu(menu, v, menuInfo);
            }
        });

        // 滚动事件
        gridView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (onScrollListener != null) {
                    onScrollListener.onScrollStateChanged(view, scrollState);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if ((firstVisibleItem + visibleItemCount == totalItemCount) && (totalItemCount != 0)) {
                    AbstractGridView.this.onScrollToEnd(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }

                if (onScrollListener != null) {
                    onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
                }
            }
        });
    }

    /**
     * Get visibility
     */
    public int getVisibility() {
        if (ptrGridView != null) {
            return ptrGridView.getVisibility();
        } else {
            return gridView.getVisibility();
        }
    }

    /**
     * Set visibility
     */
    public void setVisibility(int visibility) {
        if (ptrGridView != null) {
            ptrGridView.setVisibility(visibility);
            gridView.setVisibility(visibility);
        } else {
            gridView.setVisibility(visibility);
        }
    }

    /**
     * Get adapter
     */
    public AbstractGridAdapter<?> getAdapter() {
        return (AbstractGridAdapter<?>) gridView.getAdapter();
    }

    /**
     * Set adapter
     */
    public void setAdapter(AbstractGridAdapter<?> gridAdapter) {
        gridView.setAdapter(gridAdapter);
    }

    /**
     * Set OnItemClickGridener
     */
    public abstract void onItemClickListener(AdapterView<?> parent, View view, int position, long id);

    /**
     * Set OnItemLongClickGridener
     */
    public abstract boolean onItemLongClickListener(AdapterView<?> parent, View view, int position, long id);

    /**
     * OnCreateContextMenu
     */
    public abstract void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);

    /**
     * OnScrollGridener Scroll to end
     */
    public void onScrollToEnd(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    }

    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }

    public void setTag(final Object tag) {
        gridView.setTag(tag);
    }

    public final View findViewWithTag(Object tag) {
        return gridView.findViewWithTag(tag);
    }

    public interface OnScrollListener {

        void onScrollStateChanged(AbsListView view, int scrollState);

        void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount);
    }
}